import json

from rest_framework import serializers
from rest_framework.viewsets import ModelViewSet
from labor_price.models import ProjectList
from rest_framework.decorators import action
from rest_framework.response import Response

class ProjectListSerializer(serializers.ModelSerializer):
    class Meta:
        model=ProjectList
        fields='__all__'

class ProjectListView(ModelViewSet):
    queryset = ProjectList.objects.all()  # 查询集
    serializer_class = ProjectListSerializer  # 序列化器

    @action(methods=['get'], detail=False)
    def batch_del(self, request):
        del_list = request.GET.get('del_list')
        del_list = json.loads(del_list)
        try:
            for i in (del_list):
                ProjectList.objects.get(id=i).delete()
        except:
            return Response({'msg': "批量删除失败", 'code': 400,'data':'Template有关联数据'})
        return Response({'msg': "批量删除成功", 'code': 200})